#include "stdlib.h"
#include "stdio.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
#include "bmp.h"
#include "hx711.h"
#include "hi_timer.h" 

#define MEDICAL_MAX 3

typedef struct{
	u8 Cur_Index;//当前索引项
	u8 previous;//上一页
	u8 next;//下一页
	u8 enter;//确认
	u8 back;//返回
	void (*current_operation)();//	当前索引执行的函数(界面)
}Main_Menu; 


typedef struct{
	char* medical_name;
	char* part_a_name;
	u8 quality_a;
	char* part_b_name;
	u8 quality_b;
}Medical_Menu;

int KEY_PREVIOUS=4;
int KEY_ENTER=5;
int KEY_NEXT=6;
int KEY_BACK=10; 

extern g_weight;


enum{
	_TEST_MainPageA1=0,
	_TEST_MainPageA2,
	_TEST_MainPageA3,

	_OptionTimeC1,
	_OptionTimeC2,
	_OptionTimeC3,

	_oper_time,
	_timeover_main,

	_Choose_Medic_Main,
	_Calculate,
};

static u8 func_index = _TEST_MainPageA1;			   //当前页面索引值
static u8 last_index = _TEST_MainPageA2;			   //上一个界面索引值
static void (*current_operation_func)(); 			   //定义一个函数指针

int time_count=0; //定时
int timerCount=0;	//定时器中的计数量
char flag=0;
int current=0;
int medic_count=0;

Main_Menu table[20] ={
		
		{_TEST_MainPageA1,_TEST_MainPageA3,_TEST_MainPageA2,0,0,TEST_MainPageA1},	//0
        {_TEST_MainPageA2,_TEST_MainPageA1,_TEST_MainPageA3,_Choose_Medic_Main,0,TEST_MainPageA2},	//1
        {_TEST_MainPageA3,_TEST_MainPageA2,_TEST_MainPageA1,_OptionTimeC1,0,TEST_MainPageA3}, //2

		{_OptionTimeC1,_OptionTimeC2,_OptionTimeC3,_oper_time,0,OptionTimeC1}, //3
		{_OptionTimeC2,_OptionTimeC3,_OptionTimeC1,_oper_time,0,OptionTimeC2}, //4
		{_OptionTimeC3,_OptionTimeC1,_OptionTimeC2,_oper_time,0,OptionTimeC3}, //5

		{_oper_time,_oper_time,_oper_time,_oper_time,0,oper_time}, //6
		{_TEST_MainPageA1,_TEST_MainPageA1,_TEST_MainPageA1,_TEST_MainPageA1,_TEST_MainPageA1,timeover_main}, //7

		{_Choose_Medic_Main,_Choose_Medic_Main,_Choose_Medic_Main,_Calculate,0,Choose_Medic_Main},//8
		{_Calculate,_Calculate,_Calculate,_Calculate,0,Calculate},//9
};

Medical_Menu medical[20]={
	//药方 成分 质量 成分 质量
	{"MedicalA","waterA",560,"waterB",300},
	{"MedicalB","waterA",460,"waterB",300},
	{"MedicalC","waterA",520,"waterB",300},
};

int key_val=0;

/**
 * @name: GUI_Refresh
 * @Date: 2022-07-23 11:49:05
 * @Description: 获取刷新菜单
 * @return {*}
 */
void GUI_Refresh(void)
{
	switch (key_val){
		case '8':
			func_index = table[func_index].previous; //更新索引值
			medic_count++;
			OLED_Clear(0);   
			key_val=0;
			break;
		case '5':
			func_index = table[func_index].enter; //更新索引值
			OLED_Clear(0);   
			key_val=0;
			break;
		case '2':
			func_index = table[func_index].next; //更新索引值
			medic_count--;
			OLED_Clear(0);   
			key_val=0;
			break;
		case '4':
			func_index = table[func_index].back; //更新索引值
			OLED_Clear(0);   
			key_val=0;
			break;
		default:
			break;
	}
	current_operation_func = table[func_index].current_operation;

	(*current_operation_func)(); //执行当前索引对应的函数
} 

/**
 * @name: TEST_MainPageA
 * @Date: 2022-07-23 09:45:36
 * @Description: 主菜单A
 * @return {*}
 */
void TEST_MainPageA1(void)
{	
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowCHinese(28,0,16,"选择药物",1);
	GUI_ShowCHinese(28,16,16,"功能测试",1);
	GUI_ShowCHinese(28,32,16,"选择定时",1);

	GUI_ShowCHinese(28,48,16,"现重",1);
	GUI_ShowNum(65,48,g_weight,3,16,1); //测试用
	GUI_ShowString(96,48,"g",16,1);
	delay_ms(1500);		
}

void TEST_MainPageA2(void)
{	
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowCHinese(28,0,16,"选择定时",1);
	GUI_ShowCHinese(28,16,16,"选择药物",1);
	GUI_ShowCHinese(28,32,16,"功能测试",1);
	medic_count=0;
	GUI_ShowCHinese(28,48,16,"现重",1);
	GUI_ShowNum(65,48,g_weight,3,16,1); //测试用
	GUI_ShowString(96,48,"g",16,1);
	delay_ms(1500);		
}

void TEST_MainPageA3(void)
{	
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowCHinese(28,0,16,"功能测试",1);
	GUI_ShowCHinese(28,16,16,"选择定时",1);
	GUI_ShowCHinese(28,32,16,"选择药物",1);

	GUI_ShowCHinese(28,48,16,"现重",1);
	GUI_ShowNum(65,48,g_weight,3,16,1); //测试用
	GUI_ShowString(96,48,"g",16,1);
	delay_ms(1500);		
}

void Choose_Medic_Main()
{
	flag=0;
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);

	for(current=0;current<3;current++){
		if(medic_count+current<MEDICAL_MAX) {
			GUI_ShowString(28,current*16,medical[medic_count+current].medical_name,16,1);
			printf("%s\n",medical[medic_count+current].medical_name);
		}
		else {
			GUI_ShowString(28,current*16,medical[medic_count+current-MEDICAL_MAX].medical_name,16,1);
			printf("%s\n",medical[medic_count+current-MEDICAL_MAX].medical_name);
		}
	}
	printf("over");
}

void Calculate()
{
	if(!flag){
		GUI_ShowString(28,current*16,medical[1+medic_count].part_a_name,16,1);
		GUI_ShowNum(65,16,medical[1+medic_count].quality_a,3,16,1); 
	}
	else{
		GUI_ShowString(28,current*16,medical[1+medic_count].part_b_name,16,1);
		GUI_ShowNum(65,16,medical[1+medic_count].quality_b,3,16,1); 
	}
	GUI_ShowCHinese(28,16,16,"应重",1);
	GUI_ShowString(96,16,"g",16,1);
	GUI_ShowCHinese(28,32,16,"现重",1);
	GUI_ShowNum(65,32,g_weight,3,16,1); 
	GUI_ShowString(96,32,"g",16,1);

	//给一个浮动的误差区间+-10g 成分过多时此处可修改成switch的形式 下一版再改.
	if(flag==0){
		printf("A:%d",medical[1+medic_count].quality_a);
		if((g_weight<medical[1+medic_count].quality_a+10)&&(g_weight>medical[1+medic_count].quality_a-10)){
			GUI_ShowString(96,48,"OK",16,1);
			beep_out();
			sleep(2);
			flag=1;
			OLED_Clear(0);
		}
	}
	else if(flag==1){
		printf("B:%d",medical[1+medic_count].quality_b);
		if((g_weight<medical[1+medic_count].quality_b+10)&&(g_weight>medical[1+medic_count].quality_b-10)){
			GUI_ShowString(96,48,"OK",16,1);
			beep_out();
			sleep(2);
			func_index=0;
			OLED_Clear(0);
		}
	}
}

//定时选项 方便演示均设置为固定秒数间隔 后期修改自定义方式做成数字时钟类型
void OptionTimeC1(void)
{
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowString(28,0,"5s",16,1);
	GUI_ShowString(28,16,"10s",16,1);
	GUI_ShowString(28,32,"15s",16,1);
	time_count=10;
	timerCount=0;
}

void OptionTimeC2(void)
{
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowString(28,0,"10s",16,1);
	GUI_ShowString(28,16,"15s",16,1);
	GUI_ShowString(28,32,"5s",16,1);
	time_count=15;
	timerCount=0;
}

void OptionTimeC3(void)
{
	GUI_ShowString(12,16,">",16,1);
	GUI_ShowString(98,16,"<",16,1);
	GUI_ShowString(28,0,"15s",16,1);
	GUI_ShowString(28,16,"5s",16,1);
	GUI_ShowString(28,32,"10s",16,1);
	time_count=5;
	timerCount=0;
}

void timer_callback()
{
    unsigned long tick_last;
    timerCount++;
    tick_last = hi_get_tick();
    printf("timer1Count : %d \r\n",timerCount);
    printf("tick_last1 : %d \r\n",tick_last);
}


void oper_time()	//开始计时状态
{
	OLED_Clear(0);
    int timer_id1;
    hi_timer_create(&timer_id1);
    hi_timer_start(timer_id1,HI_TIMER_TYPE_PERIOD,1000,timer_callback,0);
    hi_sleep(300);
    while(timerCount!=time_count){
		GUI_ShowString(28,0,"Wait:",16,1);
		GUI_ShowNum(32,16,time_count-timerCount,3,16,1);
		printf("wait:%d",time_count-timerCount);
	}


	hi_timer_stop(timer_id1);
    printf("stop timer_id1 success \r\n");
    hi_timer_delete(timer_id1);
    printf("delete timer_id1 success \r\n");
	func_index=_timeover_main;

}

void timeover_main()
{
	OLED_Clear(0);
	GUI_ShowString(28,0,"OVER",16,1);
	GUI_ShowString(28,16,"OVER",16,1);
	GUI_ShowString(28,32,"OVER",16,1);
	beep_out();
}

